<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Libs\WeChatPublic;

/* 登录成功通知任务 */

class LoginNoticeJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $data_login_notice_job;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data_login_notice_job)
    {
        $this->data_login_notice_job = $data_login_notice_job;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $wx = new WeChatPublic();
        $wx->sendTemplateMessage($this->getDataArray());
    }

    //获取发送数据数组
    private function getDataArray()
    {
        // info('getDataArray 数据上传');//该方法会将日志全部记录在 storage/logs/laravel.log 文件里，这样的问题是当日志文件比较多的时候查找起来比较麻烦
        $data = [
            'touser' =>  $this->data_login_notice_job['open_id'], //要发送给用户的openid
            'template_id' => "VACnMWayvBB4Kj6a6Xe-KLQd-R5zim-e-u4xYbQKtcY", //模板标题--登录成功通知
            // 'url' => "http://39.105.228.207:8081/hbjc/index/index", //自己网站链接url
            'data' => [
                'first' => [
                    'value' => "您的账号刚刚登录了工器具检测系统",
                    'color' => "#565656"
                ],
                'keyword1' => [
                    'value' => $this->data_login_notice_job['username'],
                    'color' => "#087CF5"
                ],
                'keyword2' => [
                    'value' => "账号密码登录",
                    'color' => "#087CF5"
                ],
                'keyword3' => [
                    'value' => date("Y-m-d H:i:s"),
                    'color' => "#087CF5"
                ],
                'keyword4' => [
                    'value' => \App\Libs\Helper::getAddrByIp($this->data_login_notice_job['ip']),
                    'color' => "#087CF5"
                ],
                'remark' => [
                    'value' => "如非本人操作，请及时修改密码！\n建议使用微信扫码登录！",
                    'color' => "#3d3d3d"
                ],
            ]
        ];
        return $data;
    }
}
